package com.br.command;

import java.util.Properties;

import org.apache.log4j.Logger;

public class CommandFactory {

	private static Properties comandos = new Properties();
	
	private static CommandFactory instancia;
	
	private Logger logger = Logger.getLogger(this.getClass());

	private CommandFactory() {
		try {
			comandos.load(CommandFactory.class.getClassLoader().getResourceAsStream("commands.properties"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static CommandFactory getInstancia(){
		if(instancia==null){
			instancia = new CommandFactory();
		}
		return instancia;
	}
	
	/**
	 * cria uma instancia da classe por reflection
	 * @param name - classe que sera procurada no commands.properties
	 * @return
	 */
	public BaseCommand create(String name) {
		
		BaseCommand command = null;
		String stringClasse = comandos.getProperty(name);
		
		try {
			Class classe = Class.forName(stringClasse);
			Object object = classe.newInstance();
			command = (BaseCommand) object;
		} catch (Exception ex) {
			logger.error("Erro buscando command", ex);
		}
		
		return command;
	}

}
